import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'

import EmployeeView from '@/views/EmployeeView.vue'

import OrderView from '@/views/OrderView.vue'
import RoomView from '@/views/RoomView.vue'
import UserView from '@/views/UserView.vue'

import IndexView from '@/views/IndexView.vue'
import ShowView from '@/views/ShowView.vue'
import LoginView from '@/views/LoginView.vue'
import DeptView from '@/views/DeptView.vue'
import InformationView from '@/views/informationView.vue'
import { useTokenStore } from '@/stores/token'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      name:'login',
      component:LoginView
    },{
      path: '/',
      name: 'home',
      component: HomeView,
      redirect:'/index',
      children:[
        {
          path:'/information',
          component:InformationView
        },
        {
          path:'/dept',
          component:DeptView
        },
        {
          path:'/index',
          component:IndexView
        },{
          path:'/employee',
          component:EmployeeView
        },{
          path:'/order',
          component:OrderView
        },{
          path:'/room',
          component:RoomView
        },{
          path:'/User',
          component:UserView
        },{
          path:'/show',
          component:ShowView
        }
      ]
    }
  ]
})
//导航守卫
router.beforeEach((to,from) => {
  //to要访问的位置
  //from起始位置
  if(to.path == '/login'){
    return true;
  }else{
    const tokenStore = useTokenStore();
    if(!tokenStore.tokenStr){
      return "/login";
    }else{
      return true;
    }
  }
})

export default router
